#-*-coding:utf-8-*-
import sys
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2.QtCore import *
# import maya.cmds as cmds

class AttributsControl(QDialog):

    def __init__(self):
        super(AttributsControl, self).__init__()

        self.setWindowTitle('Attributs Control')
        self.main_layout=QVBoxLayout()
        self.main_layout.setSpacing(20)
        self.main_label = QLabel(u'对选中的物体进行属性设置')
        self.select_btn_layout=QHBoxLayout()
        self.select_mesh_light_btn=QPushButton(u'选择所有mesh light物体')
        self.select_mesh_light_btn.clicked.connect(self._get_all_meshlight)
        self.select_btn_layout.addWidget(self.select_mesh_light_btn)
        self.bool_layout=QHBoxLayout()
        self.bool_label=QLabel(u'开关属性:')
        self.bool_attribute_edit=QLineEdit()
        self.bool_radio=QRadioButton()
        self.bool_radio.clicked.connect(self._change_raido)
        self.bool_layout.addWidget(self.bool_label)
        self.bool_layout.addWidget(self.bool_attribute_edit)
        self.bool_layout.addWidget(self.bool_radio)


        self.float_layout=QHBoxLayout()
        self.attr_label=QLabel(u'数值属性:')
        self.attr_edit=QLineEdit()
        self.value_label=QLabel(u'值:')
        self.value_edit=QLineEdit()
        self.value_btn=QPushButton(u'确认')
        self.value_btn.clicked.connect(self._confirm)
        self.float_layout.addWidget(self.attr_label)
        self.float_layout.addWidget(self.attr_edit)
        self.float_layout.addWidget(self.value_label)
        self.float_layout.addWidget(self.value_edit)
        self.float_layout.addWidget(self.value_btn)

        self.color_layout = QHBoxLayout()
        self.color_label = QLabel(u'颜色属性:')
        self.color_edit = QLineEdit()
        self.r_edit = QLineEdit()
        self.r_edit.setFixedWidth(50)
        self.g_edit = QLineEdit()
        self.g_edit.setFixedWidth(50)
        self.b_edit = QLineEdit()
        self.b_edit.setFixedWidth(50)
        self.color_btn = QPushButton(u'确认')
        self.color_btn.clicked.connect(self._color_confirm)
        self.color_layout.addWidget(self.color_label)
        self.color_layout.addWidget(self.color_edit)
        self.color_layout.addWidget(self.r_edit)
        self.color_layout.addWidget(self.g_edit)
        self.color_layout.addWidget(self.b_edit)
        self.color_layout.addWidget(self.color_btn)

        self.str_layout = QHBoxLayout()
        self.str_label = QLabel(u'字符属性')
        self.str_edit = QLineEdit()
        self.str_value_label = QLabel(u'值')
        self.str_value_edit = QLineEdit()
        self.str_btn = QPushButton(u'确认')
        self.str_btn.clicked.connect(self._str_confirm)
        self.str_layout.addWidget(self.str_label)
        self.str_layout.addWidget(self.str_edit)
        self.str_layout.addWidget(self.str_value_label)
        self.str_layout.addWidget(self.str_value_edit)
        self.str_layout.addWidget(self.str_btn)

        self.help_text = QTextBrowser()
        self.init_help()
        self.main_layout.addLayout(self.select_btn_layout)
        self.main_layout.addLayout(self.bool_layout)
        self.main_layout.addLayout(self.float_layout)
        self.main_layout.addLayout(self.color_layout)
        self.main_layout.addLayout(self.str_layout)
        self.main_layout.addWidget(self.help_text)

        self.setLayout(self.main_layout)

    def _get_all_meshlight(self):
        """
        获取所有mesh light
        :return:
        """
        aiTranslator_list=['procedural','polymesh','mesh_light']
        data={}  #{'geo1':{'aiTranslator':'mesh_light','lightVisible':1},...}
        allshape = cmds.ls(type='mesh')  # 就是shape名称
        all_mesh_light = []
        # print allmesh
        for shape in allshape:
            at = shape + '.aiTranslator'
            t = cmds.getAttr(at)
            if t == 'mesh_light':
                # cmds.select(shape)
                all_mesh_light.append(shape)
                # per_data={}
                # all_mesh_light.append(shape)
                # vis=shape+'.lightVisible'
                # lightVisible=cmds.getAttr(vis)
                # per_data['aiTranslator']=2#'mesh_light' #获取的就是mesh_light
                # per_data['lightVisible']=lightVisible
                # per_data['inlayer']=0
                # data[shape]=per_data
        cmds.select(all_mesh_light)
        # if data:
        #     cmds.select(all_mesh_light)
        #     self.left_list.init_meshlight(data)



    def _change_raido(self):
        if self.bool_radio.isChecked():
            attr=self.bool_attribute_edit.text()
            if attr:
                objs = cmds.ls(sl=1)
                try:
                    for i in objs:
                        cmds.setAttr(i + '.' + attr, 1)
                except Exception as _change_raido_ERR:
                    print('_change_raido_ERR:', str(_change_raido_ERR))
        else:
            attr = self.bool_attribute_edit.text()
            if attr:
                objs = cmds.ls(sl=1)
                try:
                    for i in objs:
                        cmds.setAttr(i + '.' + attr, 0)
                except Exception as _change_raido_ERR:
                    print('_change_raido_ERR:', str(_change_raido_ERR))

    def _confirm(self):
        objs=cmds.ls(sl=1)
        print(objs)
        attr=self.attr_edit.text()
        attr_value=self.value_edit.text()
        if objs and attr and attr_value:
            try:
                for i in objs:
                    cmds.setAttr(i+'.'+attr,float(attr_value))
                choose = cmds.confirmDialog(title='result', message=u'完成', icon='warning',
                                            button=['OK'], defaultButton=u'OK', cancelButton=u'OK',
                                            dismissString=u'OK')
            except Exception as _confirm_ERR:
                print('_confirm_ERR:',str(_confirm_ERR))

    def _color_confirm(self):
        objs=cmds.ls(sl=1)
        print(objs)
        attr=self.color_edit.text()
        str_r=self.r_edit.text()
        str_g=self.g_edit.text()
        str_b=self.b_edit.text()
        r = float(str_r) if str_r else 0
        g = float(str_g) if str_g else 0
        b = float(str_b) if str_b else 0

        if objs and attr:
            try:
                for i in objs:
                    cmds.setAttr(i+'.'+attr,r,g,b,type = 'double3')
                choose = cmds.confirmDialog(title='result', message=u'完成', icon='warning',
                                            button=['OK'], defaultButton=u'OK', cancelButton=u'OK',
                                            dismissString=u'OK')
            except Exception as _color_confirm_ERR:
                print('_color_confirm_ERR:',str(_color_confirm_ERR))

    def _str_confirm(self):
        objs=cmds.ls(sl=1)
        print(objs)
        attr=self.str_edit.text()
        str_value=self.str_value_edit.text()

        if objs and attr and str_value:
            try:
                for i in objs:
                    cmds.setAttr(i+'.'+attr,str_value,type = 'string')
                choose = cmds.confirmDialog(title='result', message=u'完成', icon='warning',
                                            button=['OK'], defaultButton=u'OK', cancelButton=u'OK',
                                            dismissString=u'OK')
            except Exception as _str_confirm_ERR:
                print('_str_confirm_ERR:',str(_str_confirm_ERR))


    def init_help(self):
        text = u'1.如果对开关属性设置，比如要对Opaque打勾,则选中物体后，在开关属性栏输入aiOpaque，然后再把后面圆圈打上个勾就行了\n' \
               u'2.如果要对物体的aiExposure设置为1,则选中物体后，在属性名称栏输入：aiExposure，值填为1，按对应的确定\n' \
               u'3.如果对颜色类属性设置，比如color属性要设置为(1,0,0)，则在颜色属性栏输入color,三个值分别1,0,0，再按对应的确定\n'\
               u'4.如果对字符类属性设置，比如aiTranslator属性要设置为mesh_light，则在字符属性栏输入aiTranslator,值填为mesh_light，再按对应的确定\n'
        self.help_text.setText(text)

if __name__ == '__main__':
    try:
        t.deleteLater()
    except:
        pass
    t = AttributsControl()
    try:
        # t.create()
        t.show()
    except:
        t.deleteLater()
    #
    # app=QApplication(sys.argv)
    # M=AttributsControl()
    # # M=LightList()
    # M.show()
    # sys.exit(app.exec_())


